<template>
  <div class="diamondDetails">
    <h3>钻石明细</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入用户名" v-model="search1">
          <template slot="prepend">用户名</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户id" v-model="search2">
          <template slot="prepend">用户id</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>收支类型</label>
        <el-select v-model="search3" placeholder="请选择">
          <el-option
            v-for="item in optionsReExType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond" v-if="permissions_obj.id_148||permissions_obj.id_all">
        <label>交易类型</label>
        <el-select v-model="search4" placeholder="请选择">
          <el-option
            v-for="item in optionsTransactionType"
            :key="item.id"
            :label="item.content"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入类型id" v-model="search5">
          <template slot="prepend">类型id</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>交易时间</label>
        <el-date-picker
          v-model="search6"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="userinfo.nick" label="用户名"></el-table-column>
        <el-table-column header-align="center" align="center" prop="userinfo.id" label="用户ID"></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="描述"></el-table-column>
        <el-table-column header-align="center" align="center" prop="coins_type_text" label="收支类型"></el-table-column>
        <el-table-column header-align="center" align="center" prop="type_zh" label="交易类型"></el-table-column>
        <el-table-column header-align="center" align="center" prop="relation_id" label="类型id"></el-table-column>
        <el-table-column header-align="center" align="center" prop="coins" label="金额"></el-table-column>
        <el-table-column header-align="center" align="center" prop="remain" label="余额"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="交易时间"
          sortable
        ></el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "diamondDetails",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      search4: "",
      search5: "",
      search6: "",
      //收支类型
      optionsReExType: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 1,
          label: "收入"
        },
        {
          value: 2,
          label: "支出"
        }
      ],
      //交易类型
      optionsTransactionType: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      initData: {},
      // 权限管理
       permissions:[],
      permissions_obj:{},
    };
  },
  created(){ 
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff=false;
    this.permissions.forEach((item, index) => {
      if(item==-1){
        oneOff=true;
      }else{
        this.permissions_obj['id_'+item]=item;
      }
    });
    if(oneOff){
      this.permissions_obj.id_all=true;
    }else{
      this.permissions_obj.id_all=false;
    };
  },
  mounted() {
    this.getTransTypeList();
    this.getInitData();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getInitData();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getInitData();
    },
    searchMethod() {
      this.currentPage1 = 1;
      this.getInitData();
    },
    //获取初始化列表数据
    async getInitData() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            nick: this.search1,
            user_id: this.search2,
            coins_type: this.search3,
            type: this.search4,
            relation_id: this.search5,
            stime:
              this.search6 && this.search6.length > 0 ? this.search6[0] : "",
            otime:
              this.search6 && this.search6.length > 1 ? this.search6[1] : "",
            user_code: ""
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.coinsRecord,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.coins_type == 1) {
            item.coins_type_text = "收入";
          } else if (item.coins_type == 2) {
            item.coins_type_text = "支出";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取交易类型列表
    async getTransTypeList() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.coinsDetailType,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.optionsTransactionType = res.data;
        this.optionsTransactionType.unshift({
          id: "",
          content: "全部"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
</style>